using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StudentManagementApplication
{
    public partial class FrmSubject : Form
    {
        List<Entities.Subject> subjectList = new List<Entities.Subject>();
        List<Entities.Course> courseList = new List<Entities.Course>();
        Business.SubjectService subjectService = new Business.SubjectService();
        Business.CourseService courseService = new Business.CourseService();
        Entities.Subject subject = null;
        int iIndex = 0;

        public FrmSubject()
        {
            InitializeComponent();
        }

        private void InitializeSubject()
        {
            if (dgvSubject.Rows.Count > 1)
            {
                int iCount = dgvSubject.Rows.Count - 1;
                for (int i = 0; i < iCount; i++)
                {
                    dgvSubject.Rows.RemoveAt(0);
                }
            }
            subjectList = subjectService.GetAllSubjects();
            foreach (Entities.Subject subject in subjectList)
            {
                string[] temp = { subject.SubjectID, subject.SubjectName, subject.CourseID };
                dgvSubject.Rows.Add(temp);
            }
            dgvSubject[0, iIndex].Selected = true;
        }

        private void InitializeCourse()
        {
            courseList = courseService.GetAllCourses();
            foreach (Entities.Course course in courseList)
            {
                cmbCourse.Items.Add(course.CourseID + "--" + course.CourseName);
            }
        }

        private void FindSubjectBySubjectID(string sSubjectID)
        {
            subjectList = subjectService.GetAllSubjects();
            if (dgvSubject.Rows.Count > 1)
            {
                int iCount = dgvSubject.Rows.Count - 1;
                for (int i = 0; i < iCount; i++)
                {
                    dgvSubject.Rows.RemoveAt(0);
                }
            }
            foreach (Entities.Subject subject in subjectList)
            {
                if (subject.SubjectID.ToLower().Contains(sSubjectID))
                {
                    string[] temp = { subject.SubjectID, subject.SubjectName, subject.CourseID };
                    dgvSubject.Rows.Add(temp);
                }
            }
            iIndex = 0;
            dgvSubject[0, iIndex].Selected = true;
            if (dgvSubject.Rows.Count > 1) FindSubject(dgvSubject.Rows[iIndex].Cells[0].Value.ToString());
        }

        private void FindSubject(string sSubjectID)
        {
            subject = new Entities.Subject();
            subject = subjectService.GetSubjectBySubjectID(sSubjectID);
            txtSubjectID.Text = subject.SubjectID;
            txtSubjectName.Text = subject.SubjectName;
            cmbCourse.Text = subject.CourseID;
        }

        private bool Test()
        {
            bool bResult = true;
            string sInformation = string.Empty;
            if (txtSubjectName.Text == string.Empty)
                sInformation += "SubjectName is empty" + "\n";
            if (cmbCourse.Text == string.Empty)
                sInformation += "CourseID is empty" + "\n";
            if (sInformation != string.Empty)
            {
                MessageBox.Show(sInformation, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                bResult = false;
            }
            return bResult;
        }

        private void InsertSubject()
        {
            if (Test())
            {
                Entities.Subject subject = new Entities.Subject();
                subject.SubjectName = txtSubjectName.Text;
                subject.CourseID = (cmbCourse.Text.Length > 5 ? cmbCourse.Text.Substring(0, 5) : cmbCourse.Text);
                try
                {
                    subjectService.InsertSubject(subject);
                    iIndex = dgvSubject.Rows.Count - 1;
                    InitializeSubject();
                }
                catch
                {
                    MessageBox.Show("CourseID you choose isn't legal");
                }
            }
        }

        private void UpdateSubject()
        {
            if (Test())
            {
                subject.SubjectName = txtSubjectName.Text;
                subject.CourseID = (cmbCourse.Text.Length > 5 ? cmbCourse.Text.Substring(0, 5) : cmbCourse.Text);
                try
                {
                    subjectService.UpdateSubject(subject);
                    InitializeSubject();
                }
                catch
                {
                    MessageBox.Show("CourseID you choose isn't legal");
                }
            }
        }

        private void DeleteSubject()
        {
            try
            {
                subjectService.DeleteSubject(subject.SubjectID);
                if (iIndex > 0) iIndex--;
                else iIndex = 0;
                InitializeSubject();
                FindSubject(dgvSubject.Rows[iIndex].Cells[0].Value.ToString());
            }
            catch
            {
                MessageBox.Show("You can't delete this subject because it have references to other tables", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void FrmSubject_Load(object sender, EventArgs e)
        {
            InitializeSubject();
            InitializeCourse();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (subject != null)
            {
                if (MessageBox.Show("Do you really want to delete this file?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    DeleteSubject();
                }
            }
            else
                MessageBox.Show("You should choose the class which you want to delete!");
        }

        private void dgvSubject_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvSubject.CurrentRow.Index < dgvSubject.Rows.Count - 1)
            {
                iIndex = dgvSubject.CurrentRow.Index;
                string sSubjectID = dgvSubject.CurrentRow.Cells[0].Value.ToString();
                if (rdoUpdate.Checked)
                {
                    FindSubject(sSubjectID);
                }
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FindSubjectBySubjectID(txtSubjectID.Text.ToLower());
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            InsertSubject();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (subject != null)
                UpdateSubject();
            else
                MessageBox.Show("You should choose the class which you want to change!");
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rdoUpdate_CheckedChanged(object sender, EventArgs e)
        {
            txtSubjectID.Enabled = true;
            cmbCourse.Text = string.Empty;
            txtSubjectID.Text = string.Empty;
            txtSubjectName.Text = string.Empty;
            pictureBox1.Enabled = true;
            btnInsert.Enabled = false;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            if(dgvSubject.Rows.Count>1) FindSubject(dgvSubject.Rows[iIndex].Cells[0].Value.ToString());
        }

        private void rdoInsert_CheckedChanged(object sender, EventArgs e)
        {
            txtSubjectID.Enabled = false;
            cmbCourse.Text = string.Empty;
            txtSubjectID.Text = string.Empty;
            txtSubjectName.Text = string.Empty;
            pictureBox1.Enabled = false;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }
    }
}